package com.yunxi.service.dubboService.impl.sat;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.model.sat.PartnerCustomer;
import com.yunxi.sat.pojo.PartnerCustomerVO;
import com.yunxi.sat.service.PartnerCustomerDubboService;
import com.yunxi.service.localService.sat.PartnerCustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 渠道客户关系 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-07-12
 */
public class PartnerCustomerDubboServiceImpl implements PartnerCustomerDubboService {

    @Autowired
    @Qualifier(value = "partnerCustomerServiceImpl")
    PartnerCustomerService partnerCustomerService;


    @Override
    public PartnerCustomerVO getById(String id) throws Exception {
        PartnerCustomer partnerCustomer = partnerCustomerService.getById(id);
        if(partnerCustomer != null){
            PartnerCustomerVO partnerCustomerVO = new PartnerCustomerVO();
            BeanUtils.copyProperties(partnerCustomer, partnerCustomerVO);
            return partnerCustomerVO;
        }
        else{
            throw new Exception("操作失败,无法根据id:"+id+"找到对应渠道客户关系记录!");
        }
    }

    @Override
    public void saveOrUpdate(PartnerCustomerVO partnerCustomerVO,LoginInfo loginInfo) throws Exception {
        setPartnerCode(partnerCustomerVO, loginInfo);
        partnerCustomerVO.setCreateBy(loginInfo.getUserCode());
        partnerCustomerVO.setUpdateBy(loginInfo.getUserCode());
        PartnerCustomer partnerCustomer = new PartnerCustomer();
        BeanUtils.copyProperties(partnerCustomerVO, partnerCustomer);
        partnerCustomerService.saveOrUpdate(partnerCustomer);
    }

    @Override
    public void removeById(String id) {
        partnerCustomerService.removeById(id);
    }

    @Override
    public PageInfo<PartnerCustomerVO> findPageByVO(PartnerCustomerVO partnerCustomerVO, Integer pageNum, Integer pageSize, LoginInfo loginInfo) throws Exception {
        setPartnerCode(partnerCustomerVO, loginInfo);
        PageInfo<PartnerCustomerVO> pageInfo = partnerCustomerService.pageByVo(partnerCustomerVO, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public void removeByListVo(List<PartnerCustomerVO> partnerCustomerVOList) throws Exception {
        partnerCustomerService.removeByListVo(partnerCustomerVOList);
    }

    @Override
    public List listByVo(PartnerCustomerVO partnerCustomerVO) {
        return partnerCustomerService.listByVo(partnerCustomerVO);
    }

    @Override
    public void del(List<String> list, String updateBy) throws Exception {
        if(list != null && list.size() > 0){
            for (String id : list){
                PartnerCustomer byId = partnerCustomerService.getById(id);
                if(byId != null){
                    byId.setIsDel(1);
                    byId.setUpdateBy(updateBy);
                    partnerCustomerService.saveOrUpdate(byId);
                }
                else{
                    throw new Exception("操作失败,无法根据id:"+id+"找到对应渠道客户关系记录!");
                }
            }
        }
        else{
            throw new Exception("操作失败,list为空!");
        }
    }

    /**
     * 设置渠道商代码
     * @param partnerCustomerVO
     * @param loginInfo
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private void setPartnerCode(PartnerCustomerVO partnerCustomerVO, LoginInfo loginInfo) throws IllegalAccessException, InvocationTargetException {
        Object partner = loginInfo.getPartner();
        if(partner != null){
            PartnerVO partnerVO = new PartnerVO();
            org.apache.commons.beanutils.BeanUtils.copyProperties(partnerVO,partner);
            partnerCustomerVO.setPartnerCode(partnerVO.getPartnerCode());
        }
    }
}
